package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.domain.SysCollect;
import com.ruoyi.system.domain.SysFollow;
import com.ruoyi.system.domain.SysVideo;
import com.ruoyi.system.service.BehaviorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    public StringRedisTemplate stringRedisTemplate;
    /**
     * 点赞
     * @param dto
     * @return
     */
    @Override
    public AjaxResult likesBehavior(SysVideo dto, Long userId, boolean operation) {
        // 3. 点赞, 保存数据
        if(operation){
            // 点赞
            Object obj = stringRedisTemplate.opsForValue().get("LIKE-BEHAVIOR-" + dto.getId() + "-" + userId);
            if(obj != null){
                return AjaxResult.error("已点赞");
            }
            stringRedisTemplate.opsForValue().set("LIKE-BEHAVIOR-" + dto.getId() + "-" + userId, JSON.toJSONString(dto));
        }else{
            // 取消点赞
            // 删除当前key
            stringRedisTemplate.delete("LIKE-BEHAVIOR-" + dto.getId() + "-" + userId);
        }

        return AjaxResult.success();
    }

    /**
     * 收藏
     * @param dto
     * @param userId
     * @return
     */
    @Override
    public AjaxResult collectBehavior(SysCollect dto, Long userId, boolean operation) {
        // 3. 收藏, 保存数据
        if(operation){
            // 收藏
            Object obj = stringRedisTemplate.opsForValue().get("COLLECT-BEHAVIOR-" + dto.getVideoId()+ "-" + userId);
            if(obj != null){
                return AjaxResult.error("已收藏");
            }
            stringRedisTemplate.opsForValue().set("COLLECT-BEHAVIOR-" + dto.getVideoId() + "-" + userId, JSON.toJSONString(dto));
        }else{
            // 取消收藏
            // 删除当前key
            stringRedisTemplate.delete("COLLECT-BEHAVIOR-" + dto.getVideoId() + "-" + userId);
        }

        return AjaxResult.success();
    }

    /**
     * 关注
     * @param dto
     * @param userId
     * @param operation
     * @return
     */
    @Override
    public AjaxResult followehavior(SysFollow dto, Long userId, boolean operation) {
        // 3. 关注, 保存数据
        if(operation){
            // 关注
            Object obj = stringRedisTemplate.opsForValue().get("FOLLOW-BEHAVIOR-" + dto.getFollowId()+ "-" + userId);
            if(obj != null){
                return AjaxResult.error("已关注");
            }
            stringRedisTemplate.opsForValue().set("FOLLOW-BEHAVIOR-" + dto.getFollowId() + "-" + userId, JSON.toJSONString(dto));
        }else{
            // 取消关注
            // 删除当前key
            stringRedisTemplate.delete("FOLLOW-BEHAVIOR-" + dto.getFollowId() + "-" + userId);
        }

        return AjaxResult.success();
    }

    /**
     * 加载用户行为
     * @param mp
     * @return
     */
    @Override
    public AjaxResult loadBehavior(Map<String, Object> mp) {
        int userId = (int) mp.get("userId");
        String videoId = (String) mp.get("videoId");
        int followId = (int) mp.get("followId");

        // 3. 回显数据
        Map<String, Boolean> map = new HashMap<>();
        Boolean islike = false, iscollection = false, isFollow = false;
        // 3.1 是否点赞
        String likeJson = stringRedisTemplate.opsForValue().get("LIKE-BEHAVIOR-" + videoId + "-" + userId);
        if(likeJson != null){
            islike = true;
        }
        // 3.3 是否收藏
        String collectiontJson = stringRedisTemplate.opsForValue().get("COLLECT-BEHAVIOR-" + videoId + "-" + userId);
        if(collectiontJson != null){
            iscollection = true;
        }
        // 3.3 是否关注
        String followJson = stringRedisTemplate.opsForValue().get("FOLLOW-BEHAVIOR-" + followId + "-" + userId);
        if(followJson != null){
            isFollow = true;
        }
        map.put("islike", islike);
        map.put("iscollection", iscollection);
        map.put("isFollow", isFollow);
        return AjaxResult.success(map);
    }

//    /**
//     * 阅读
//     * @param dto
//     * @return
//     */
//    @Override
//    public AjaxResult readBehavior(ReadBehaviorDto dto) {
//
//    }
//
//    /**
//     * 不喜欢
//     * @param dto
//     * @return
//     */
//    @Override
//    public AjaxResult unLikesBehavior(UnLikesBehaviorDto dto) {
//
//    }

//    /**
//     *
//     * @param dto
//     * @return
//     */
//    @Override
//    public AjaxResult collectBehavior(UnLikesBehaviorDto dto) {
//
//    }
}
